<?php
/**
 * Created by PhpStorm.
 * @description:
 * @time:
 * @Author: yfl
 * @QQ 554665488
 * Date: 2018-5-28
 * Time: 16:42
 */
$server = new swoole_server('1270.0.0.1', 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
//固定包头跟包体协议
$server->set([
    'open_length_check' => true,
    'package_length_type' => 'N',
    'package_length_offset' => 0, //计算总长度
    'package_body_offset' => 4,//包体位置
    'package_max_length' => 1024 * 1024 //总的请求数据大小字节为单位
]);
$server->on('connect', function ($serv, $fd) {

});

$server->on('receive', function ($serv, $fd, $from_id, $data) {
    $len = unpack('N', $data)[1];
    $data=substr($data,-$len);
//    var_dump($data);
    for ($i = 0; $i < 20; $i++) {
        $serv->send($fd, pack('N', strlen($data) ). $data);
    }
});

$server->on('close', function ($serv, $fd) {

});
$server->start();